/*
 * jxSwing Project (2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxSwing.component.comboBox;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import org.jxSwing.component.comboBox.cellRenderer.ItemListRenderer;

/**
 * @author Denilson Edinaldo Pinto
 * @porject jxSwing
 * @package org.jxSwing.component.comboBox
 * @date 20/02/2014 - 17:58:38
 */
public class ItemComboBox extends ComboBox<ComboItem> {
	
	private static final long serialVersionUID = -1579815092489356212L;
	
	/**
	 * 
	 */
	public ItemComboBox() {
	
		super();
		
		initGUI();
	}
	
	/**
	 * @param items
	 */
	public ItemComboBox(final ComboItem[] items) {
	
		super(items);
		
		initGUI();
	}
	
	/**
	 * @param comboItems
	 */
	public ItemComboBox(final List<ComboItem> comboItems) {
	
		super(comboItems);
		
		initGUI();
	}
	
	/**
	 * @param values
	 */
	public ItemComboBox(final Object[] values) {
	
		super(toComboItemList(values));
		
		initGUI();
	}
	
	/**
	 * 
	 */
	private void initGUI() {
	
		setRenderer(new ItemListRenderer());
	}
	
	/**
	 * @param values
	 * @return comboItems
	 */
	private static List<ComboItem> toComboItemList(final Object[] values) {
	
		final List<ComboItem> comboItems = new ArrayList<>();
		
		//
		for(final Object value : values) {
			
			comboItems.add(new ComboItem(value));
		}
		
		return comboItems;
	}
	
	/**
	 * @author Denilson Edinaldo Pinto
	 * @porject jxSwing
	 * @package org.jxSwing.component.comboBox
	 * @date 20/02/2014 - 18:08:33
	 */
	public class ComboListener implements ActionListener {
		
		private final ComboBox<ComboItem> comboBox;
		
		private Object currentItem;
		
		/**
		 * @param comboBox
		 */
		public ComboListener(final ComboBox<ComboItem> comboBox) {
		
			this.comboBox = comboBox;
			
			comboBox.setSelectedIndex(0);
			
			currentItem = comboBox.getSelectedItem();
		}
		
		/*
		 * (non-Javadoc)
		 * 
		 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
		 */
		@Override
		public void actionPerformed(final ActionEvent actionEvent) {
		
			final ComboItem tempItem = comboBox.getSelectedItem();
			
			//
			if(tempItem != null && !tempItem.isEnabled()) {
				
				comboBox.setSelectedItem(currentItem);
				
			}else {
				
				currentItem = tempItem;
			}
		}
	}
}
